package com.damionew.website.controller.back;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.damionew.website.model.back.User;
import com.damionew.website.service.back.UserService;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.github.pagehelper.PageHelper;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    //指向用户管理页面
    @RequestMapping("/index")
    public String index(User user) {
	System.out.println("指向用户管理页面");
	return "back/user/index";
    }
  //指向用户管理页面
    @RequestMapping("/loginindex")
    public String loginindex() {
	return "back_login";
    }
    //分页查询
    @ResponseBody
    @RequestMapping(value="pageInfo",produces="html/text;charset=UTF-8")
    public  String pageInfo(@RequestParam int pageNumber,int pageSize,HttpServletResponse response,String sortName,String sortOrder) {
	//数据总数，返回到前台总记录数，pageInfo为一页的数量，不可信
	List<User> users =userService.getUserList(sortName, sortOrder);
	int total = users.size();
	//startPage方法传入参数(初始页码，一页数量)，只对跟着此方法的下一条查询有效
	PageHelper.startPage(pageNumber,pageSize);
	List<User> pageInfo=userService.getUserList(sortName,sortOrder);
	//json返回每页数据和数据总数
	JSONObject result = new JSONObject();
	result.put("rows",pageInfo);
	result.put("total",total);
	return result.toJSONString();
    }
    //新增用户
    @ResponseBody
    @RequestMapping("/addUser")
    @RequiresPermissions("user:add")
    public String addUser(User user) {
	//这里添加创建时间
	Date date = new Date();
	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	String time = dateFormat.format(date);
	user.setCreated_at(time);
	//初始设置状态为1，即启用状态
	user.setStates("1");
	userService.addUser(user);
	//返回前台ajax，json：{"state":"success"},success方法调用提示框
	JSONObject result = new JSONObject();
	result.put("state", "success");
	return result.toJSONString();
    }
    //修改用户
    @ResponseBody
    @RequestMapping("/updateUser")
    @RequiresPermissions("user:update")
    public String updateUser(User user) {
	userService.updateUser(user);
	JSONObject result = new JSONObject();
	result.put("state", "success");
	return result.toJSONString();
    }
    //删除用户
    @ResponseBody
    @RequestMapping("/deleteUser")
//    @RequiresRoles("sa")
    @RequiresPermissions("user:delete")
    public String deleteUser(HttpServletRequest request) {
	//获取前台传送的数组，并for循环删除
	String[] list=request.getParameterValues("ids");
	for (int i = 0; i < list.length; i++) {
	    String id = list[i];
	    userService.deleteUser(id);
	}
	JSONObject result = new JSONObject();
	result.put("state", "success");
	return result.toJSONString();
    }
}
